<script setup lang="ts">
import { ref } from "vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import { useDict } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "SystemViewsShow"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,viewList,switchDict,selectCateId,roleList,userList,
  treeData,
  treeLoading,
  selectedNum,
  pagination,
  buttonClass,
  deviceDetection,
  onSearch,
  resetForm,
  onbatchDel,
  openDialog,
  onTreeSelect,
  handleUpdate,
  handleDelete,
  handleUpload,
  handleReset,
  handleSizeChange,
  onSelectionCancel,
  handleCurrentChange,
  handleSelectionChange
} = useDict(tableRef);
</script>

<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <div
      :class="['mr-2', 'bg-bg_color',deviceDetection() ? 'w-full' : 'min-w-[200px]']"
    >
      <h5 class="mb-2" style="background-color: #f5f7fa; height:3rem;line-height: 3rem;text-align: center">视图分类</h5>
      <el-menu
        default-active="0" background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        class="el-menu-vertical-demo"
      >
        <el-menu-item index="0" @click="switchDict(0)">
          <span :class="selectCateId==0?'selectItem':''">全部</span>
        </el-menu-item>
        <el-menu-item :index="item.id" v-for="item in viewList" @click="switchDict(item.id)"  >
          <span :class="selectCateId==item.id?'selectItem':''"> {{ item.label }}</span>
        </el-menu-item>
      </el-menu>



    </div>
    <div
      :class="[deviceDetection() ? ['w-full', 'mt-2'] : 'w-[calc(100%-200px)]']"
    >
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="" prop="view_name">
          <el-input
            v-model="form.view_name"
            placeholder="请输入视图名称"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="useRenderIcon('ri:search-line')" :loading="loading" @click="onSearch"> {{ t("buttons.search") }} </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">{{ t("buttons.reset") }}</el-button>
        </el-form-item>
      </el-form>

      <PureTableBar
        title="展示视图设置"
        :columns="columns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button v-perms="'SystemViewsShow:btn:add'" type="primary" :icon="useRenderIcon(AddFill)" @click="openDialog(viewList)">
            {{ t("buttons.add") }}
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <div
            v-if="selectedNum > 0"
            v-motion-fade
            class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
          >
            <div class="flex-auto">
              <span
                style="font-size: var(--el-font-size-base)"
                class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
              >
                已选 {{ selectedNum }} 项
              </span>
              <el-button type="primary" text @click="onSelectionCancel">
                取消选择
              </el-button>
            </div>
            <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
              <template #reference>
                <el-button v-perms="'SystemViewsShow:btn:add'"  type="danger" text class="mr-1">{{ t("buttons.batchDelete") }}</el-button>
              </template>
            </el-popconfirm>
          </div>
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button v-perms="'SystemViewsShow:btn:edit'" class="reset-margin" link
                type="primary" :size="size"  :icon="useRenderIcon(EditPen)"
                @click="openDialog(viewList,'修改', row)">
                {{ t("buttons.edit") }}
              </el-button>
              <el-popconfirm :title="`是否确认删除配置编号为${row.id}的这条数据`" @confirm="handleDelete(row)">
                <template #reference>
                  <el-button v-perms="'SystemViewsShow:btn:delete'" class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(Delete)">
                    {{ t("buttons.delete") }}
                  </el-button>
                </template>
              </el-popconfirm>

            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
.selectItem{ color: var(--el-color-primary); font-weight: bold }
:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
